package com.kpmg.datalake.common.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kpmg.datalake.common.dao.ComDictMapper;
import com.kpmg.datalake.common.model.ComDict;
import com.kpmg.datalake.common.service.IComDictService;
import com.kpmg.datalake.common.utils.AssertUtil;
import com.kpmg.datalake.common.utils.ContextHolderUtils;
import com.kpmg.datalake.common.vo.ViewDictDTO;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.aop.framework.AopContext;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 字典信息明细表 服务实现类
 * </p>
 *
 * @author azhao6
 * @since 2018-08-16
 */
@Service
public class ComDictServiceImpl extends ServiceImpl<ComDictMapper, ComDict> implements
    IComDictService {

  @Override
  public List<ViewDictDTO> getViewDictListByType(String dictType) {
    return ((ComDictServiceImpl) AopContext.currentProxy())
        .getViewDictListByType(dictType, ContextHolderUtils.getLocale().toString());
  }

  @Override
  public List<ViewDictDTO> getViewDictListByType(String dictType, String locale) {
    return ((ComDictServiceImpl) AopContext.currentProxy())
        .getViewDictListByTypeAndParent(dictType, null, locale);
  }

  @Override
  public List<ViewDictDTO> getViewDictListByTypeAndParent(String dictType, String parentCode) {
    return ((ComDictServiceImpl) AopContext.currentProxy())
        .getViewDictListByTypeAndParent(dictType, parentCode,
            ContextHolderUtils.getLocale().toString());
  }

  @Override
  @Cacheable(value = "dictionary", key = "#dictType+':'+#locale+':'+#parentCode", unless = "#result == null")
  public List<ViewDictDTO> getViewDictListByTypeAndParent(String dictType, String parentCode,
      String locale) {
    AssertUtil.assertNotEmpty(dictType, "字典类型不能为空");
    AssertUtil.assertNotEmpty(locale, "语言不能为空");
    ViewDictDTO viewDictDTO = new ViewDictDTO();
    viewDictDTO.setDictType(dictType);
    viewDictDTO.setLangCd(locale.toLowerCase());
    viewDictDTO.setParentCode(parentCode);
    return baseMapper.selectViewDictList(viewDictDTO);
  }


  @Override
  public Page<ViewDictDTO> getViewDictPageByTypeAndParent(Page<ViewDictDTO> page, String dictType,
      String parentCode, String locale) {
    AssertUtil.assertNotEmpty(dictType, "字典类型不能为空");
    AssertUtil.assertNotEmpty(locale, "语言不能为空");
    ViewDictDTO viewDictDTO = new ViewDictDTO();
    viewDictDTO.setDictType(dictType);
    viewDictDTO.setLangCd(locale.toLowerCase());
    viewDictDTO.setParentCode(parentCode);
    return page.setRecords(baseMapper.selectViewDictList(page, viewDictDTO));
  }

  @Override
  public Map<String, String> getViewDictMapByTypeAndParent(String dictType, String parentCode,
      String locale) {
    List<ViewDictDTO> dictDTOList = ((ComDictServiceImpl) AopContext.currentProxy())
        .getViewDictListByTypeAndParent(dictType, parentCode, locale);
    Map<String, String> result = new HashMap<>();
    if (dictDTOList != null) {
      for (ViewDictDTO viewDictDTO : dictDTOList) {
        result.put(viewDictDTO.getDictCode(), viewDictDTO.getDictName());
      }
    }
    return result;
  }

  @Override
  public Map<String, String> getViewDictMapByType(String dictType, String locale) {
    return getViewDictMapByTypeAndParent(dictType, null, locale);
  }

  @Override
  public Map<String, String> getViewDictMapByType(String dictType) {
    return getViewDictMapByType(dictType, ContextHolderUtils.getLocale().toString());
  }
}
